<?php
namespace Admin\Controller;
// 角色模块
class RoleController extends CommonController {

	/**
	 * 角色列表
	 * @internal 此方法显示角色列表
	 * @access public
	 * @param string $numPerPage
	 * @param string $pageNum
	 * @param string $keyword 搜索关键字
	 * @since 2014-10-11 15:28:59
	 * @author FrankKung <kongfanjian@andlisoft.com>
	 */
	public function index() {
		// 获取分页等参数
		$numPerPage = I('post.numPerPage', 20);
		$pageNum    = I('post.pageNum', 1);
		$keyword    = I('post.keyword');

		// 模型
		$Role = D("Role");

		// 模糊搜索
		C('DB_LIKE_FIELDS','name'); // 设置模糊查询字段
		if (!empty($keyword)) { // 搜索关键字
			$map['name'] = $keyword;
		}

		// 获取角色列表
		$list = $Role->where($map)->select();
		$this->assign('list', $list);

		// 分页信息
		$total = $Role->where($map)->count($id);
		$this->assign('total', $total);
		$this->assign('numPerPage', $numPerPage);
		$this->assign('pageNum', $pageNum);

		// 显示
		$this->display();
	}

	/**
	 * 新增角色
	 * @internal 此方法用于新增角色
	 * @access public
	 * @param string $name
	 * @param string $status
	 * @param string $remark
	 * @param string $pid
	 * @since 2014-10-13 10:42:56
	 * @author FrankKung <kongfanjian@andlisoft.com>
	 */
	public function add() {
		// 模型
		$Role = D("Role");

		if (!IS_POST) { // 显示新增页面
			$this->display();
		} else {
			// 表单数据
			$data = array(
				'name'		=> I('post.name'),
				'status'	=> I('post.status'),
				'remark'	=> I('post.remark'),
				'createTime'=> time(),
			);
			
			$id = $Role->add($data);
			if (false !== $id) {
				$this->DwzCallback('增加角色成功');
			} else {
				$this->DwzCallback('增加角色失败', '', 300);
			}
		}
	}

	/**
	 * 删除角色
	 * @internal 此方法用于新增角色
	 * @access public
	 * @param string $id
	 * @since 2014-10-13 11:10:49
	 * @author FrankKung <kongfanjian@andlisoft.com>
	 */
	public function delete() {
		// 模型
		$Role = D("Role");
		// 条件
		$map['id'] = I('get.id', '0');

		$ret = $Role->where($map)->delete();
		if (false !== $ret) {
			$this->DwzCallback('删除角色成功');
		} else {
			$this->DwzCallback('删除角色失败', '', 300);
		}
	}

	/**
	 * 编辑角色
	 * @internal 此方法用于编辑角色
	 * @access public
	 * @param string $id
	 * @since 2014-10-13 10:57:18
	 * @author FrankKung <kongfanjian@andlisoft.com>
	 */
	public function edit() {
		// 参数
		$id = I('get.id', '0');
		// 模型
		$Role = D("Role");
		// 条件
		$map['id'] = $id;

		if (!IS_POST) { // 显示编辑页面
			$info = $Role->where($map)->find();
			$this->assign('info', $info);
			$this->display();
		} else { // 保存数据
			// 表单数据
			$data = array(
				'name'			=> I('post.name'),
				'status'		=> I('post.status'),
				'remark'		=> I('post.remark'),
				'updateTime'	=> time(),
			);
			$ret = $Role->where($map)->save($data);
			if (false !== $ret) {
				$this->DwzCallback('编辑角色成功');
			} else {
				$this->DwzCallback('编辑角色失败', '', 300);
			}
		}
	}

	/**
	 * 选择用户角色
	 * @internal 此方法用于选择用户角色
	 * @access public
	 * @param string $id 角色ID
	 * @since 2014-10-13 11:19:58
	 * @author FrankKung <kongfanjian@andlisoft.com>
	 */


/////////////////////////////////////////////////////////////
	/**
	 * 组操作权限列表
	 *
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function app()
	{
		//读取系统的项目列表
		$node    =  D("Node");
		$list	=	$node->where('level=1')->field('id,title')->select();
		foreach ($list as $vo){
			$appList[$vo['id']]	=	$vo['title'];
		}

		//读取系统组列表
		$group   =  D('Role');
		$list       =  $group->field('id,name')->select();
		foreach ($list as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

		//获取当前用户组项目权限信息
		$groupId =  isset($_GET['groupId'])?$_GET['groupId']:'';
		$groupAppList = array();
		if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
			//获取当前组的操作权限列表
			$list	=	$group->getGroupAppList($groupId);
			foreach ($list as $vo){
				$groupAppList[$vo['id']]	=	$vo['id'];
			}
		}
		$this->assign('groupAppList',$groupAppList);
		$this->assign('appList',$appList);
		$this->display();

		return;
	}

	 /**
	 * 增加组操作权限
	 *
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function setModule()
	{
		$id     = $_POST['groupModuleId'];
		$groupId	=	$_POST['groupId'];
		$appId	=	$_POST['appId'];
		$group    =   D("Role");
		$group->delGroupModule($groupId,$appId);
		$result = $group->setGroupModules($groupId,$id);

		if($result===false) {
			$this->error('模块授权失败！');
		}else {
			$this->success('模块授权成功！');
		}
	}


	/**
	 * 组操作权限列表
	 *
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function module()
	{
		$groupId =  $_GET['groupId'];
		$appId  = $_GET['appId'];

		$group   =  D("Role");
		//读取系统组列表
		$list=$group->field('id,name')->select();
		foreach ($list as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

		if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
			//读取系统组的授权项目列表
			$list	=	$group->getGroupAppList($groupId);
			foreach ($list as $vo){
				$appList[$vo['id']]	=	$vo['title'];
			}
			$this->assign("appList",$appList);
		}
		$node    =  D("Node");
		if(!empty($appId)) {
			$this->assign("selectAppId",$appId);
			//读取当前项目的模块列表
			$where['level']=2;
			$where['pid']=$appId;
			$nodelist=$node->field('id,title')->where($where)->select();
			foreach ($nodelist as $vo){
				$moduleList[$vo['id']]	=	$vo['title'];
			}
		}

		//获取当前项目的授权模块信息
		$groupModuleList = array();
		if(!empty($groupId) && !empty($appId)) {
			$grouplist	=	$group->getGroupModuleList($groupId,$appId);
			foreach ($grouplist as $vo){
				$groupModuleList[$vo['id']]	=	$vo['id'];
			}
		}

		$this->assign('groupModuleList',$groupModuleList);
		$this->assign('moduleList',$moduleList);

		$this->display();

		return;
	}

	 /**
	 * 增加组操作权限
	 *
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function setAction()
	{
		$id     = $_POST['groupActionId'];
		$groupId	=	$_POST['groupId'];
		$moduleId	=	$_POST['moduleId'];
		$group    =   D("Role");
		$group->delGroupAction($groupId,$moduleId);
		$result = $group->setGroupActions($groupId,$id);

		if($result===false) {
			$this->error('操作授权失败！');
		}else {
			$this->success('操作授权成功！');
		}
	}


	/**
	 * 组操作权限列表
	 *
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function action()
	{
		$groupId =  $_GET['groupId'];
		$appId  = $_GET['appId'];
		$moduleId  = $_GET['moduleId'];

		$group   =  D("Role");
		//读取系统组列表
		$grouplist=$group->field('id,name')->select();
		foreach ($grouplist as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

		if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
			//读取系统组的授权项目列表
			$list	=	$group->getGroupAppList($groupId);
			foreach ($list as $vo){
				$appList[$vo['id']]	=	$vo['title'];
			}
			$this->assign("appList",$appList);
		}
		if(!empty($appId)) {
			$this->assign("selectAppId",$appId);
			//读取当前项目的授权模块列表
			$list	=	$group->getGroupModuleList($groupId,$appId);
			foreach ($list as $vo){
				$moduleList[$vo['id']]	=	$vo['title'];
			}
			$this->assign("moduleList",$moduleList);
		}
		$node    =  D("Node");

		if(!empty($moduleId)) {
			$this->assign("selectModuleId",$moduleId);
			//读取当前项目的操作列表
			$map['level']=3;
			$map['pid']=$moduleId;
			$list	=	$node->where($map)->field('id,title')->select();
			if($list) {
				foreach ($list as $vo){
					$actionList[$vo['id']]	=	$vo['title'];
				}
			}
		}


		//获取当前用户组操作权限信息
		$groupActionList = array();
		if(!empty($groupId) && !empty($moduleId)) {
			//获取当前组的操作权限列表
			$list	=	$group->getGroupActionList($groupId,$moduleId);
			if($list) {
			foreach ($list as $vo){
				$groupActionList[$vo['id']]	=	$vo['id'];
			}
			}

		}

		$this->assign('groupActionList',$groupActionList);
		//$actionList = array_diff_key($actionList,$groupActionList);
		$this->assign('actionList',$actionList);

		$this->display();

		return;
	}

	/**
	 * 增加组操作权限
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function setUser(){
		// var_export($_POST);die;
		$id     = $_POST['groupUserId'];//用户id的一个 数组
		$groupId	=	$_POST['groupId'];//组名
		
		$group    =   D("Role");
		$group->delUserGroup($id);//清空所有成员的权限
		$group->delGroupUser($groupId);//清空该组的所有成员
		$result = $group->setGroupUsers($groupId,$id);//将成员加入进来
		if($result===false) {
			$this->DwzCallback('授权失败！', '', 300);
		}else {
			$this->DwzCallback('授权成功！','',200);
		}
	}

	/**
	 * 组操作权限列表
	 * @access public
	 * @return void
	 * @throws FcsException
	 */
	public function user()
	{
		//读取系统的用户列表
		$user    =   D("User");
		$list2=$user->field('id,account,nickname')->select();
		//echo $user->getlastsql();
		//dump(	$user);
		foreach ($list2 as $vo){
			$userList[$vo['id']]	=	$vo['account'].' '.$vo['nickname'];
		}

		$group    =   D("Role");
		$list=$group->field('id,name')->select();
		foreach ($list as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}
		$this->assign("groupList",$groupList);

		//获取当前用户组信息
		$groupId =  isset($_GET['id'])?$_GET['id']:'';
		$groupUserList = array();
		if(!empty($groupId)) {
			$this->assign("selectGroupId",$groupId);
			//获取当前组的用户列表
			$list	=	$group->getGroupUserList($groupId);
			foreach ($list as $vo){
				$groupUserList[$vo['id']]	=	$vo['id'];
			}

		}
		$this->assign('groupUserList',$groupUserList);
		$this->assign('userList',$userList);
		$this->display();

		return;
	}
/*	
	public function _before_edit(){
	   $Group = D('Role');
		//查找满足条件的列表数据
		$list     = $Group->field('id,name')->select();
		$this->assign('list',$list);

	}
	public function _before_add(){
	   $Group = D('Role');
		//查找满足条件的列表数据
		$list     = $Group->field('id,name')->select();
		$this->assign('list',$list);

	}
*/
	public function select()
	{
		$map = $this->_search();
		//创建数据对象
		$Group = D('Role');
		//查找满足条件的列表数据
		$list     = $Group->field('id,name')->select();
		$this->assign('list',$list);
		$this->display();
		return;
	}





	/*
		囚鸟先生扩展
	*/

	public function addAccess(){
		$node=M("Node")->select();
		$nodeData=list_to_tree($node);
		$this->node=$nodeData;

		$groupId=$_GET['groupId'];
		$Access=M("Access")->where(array('role_id'=>"{$groupId}"))->select();

		$array=array();
		foreach($Access as $val){
			$array[$val['level']][]=$val['node_id'];
		}

		//dump($node);
		$this->selectdNode=$array;
		$this->display();
	}


	public function insertAccess(){
		$role_id=$_POST['role_id'];
		$data=$dataNode=array();
		M("Access")->where(array('role_id'=>"$role_id"))->delete();
		foreach($_POST['node_id'] as $val){
			$postNode=explode('_', $val);
			$dataNode['role_id']=$role_id;
			$dataNode['node_id']=$postNode['0'];
			$dataNode['level']=$postNode['1'];
			$dataNode['pid']=$postNode['2'];
			$data[]=$dataNode;
		}
		$accNum=M("Access")->addAll($data);
		// if($accNum){
			// $this->success('授权成功。');
		// }else{
			// $this->error('授权失败。');
		// }
		if(empty($accNum)) {
			$this->DwzCallback('授权失败！', '', 300);
		}else {
			$this->DwzCallback('授权成功！','',200);
		}
	}

	function foreverdelete(){
		$id = I('get.id');
		$model = D('Role');
		$map['id'] = $id;
		$r = $model->where($map)->delete();
		/*if( $r ){
			
		}else{
			$this->DwzCallback('删除失败，用户列表未清空', '', 300);
		}*/
		$this->DwzCallback('删除成功', '', 200);
	}
}
?>